close all;
[val, text, raw] = xlsread('VNIBOR.xlsx',10);
ncol = size(val,2);
%%
Standardize = false;
col = [4 5 7 ];


Y = val(:,2);
X = val(:,col);

N = size(val,1);
Ntrain = floor(N.*3./4);
Ntest = size(val,1) - Ntrain;

Xtrain = X(1:Ntrain,:);
Ytrain = Y(1:Ntrain);
% dates = datenum(text(2:end,1));


%%

rng(1);
c = cvpartition(Ntrain,'KFold',10);
opts = optimset('TolX',5e-4,'TolFun',5e-4);

% loose grid
xmin = 1;xmax = 3;
ymin = -6; ymax = -4;
dx = 1/5; dy = 1/5;

% fine grid
% xmin = -1;xmax = 0;
% ymin = -1; ymax = 0;
% dx = 0.2; dy = 0.2;

nx = (xmax - xmin)/dx +1;
ny = (ymax - ymin)/dy +1;
fres = zeros(nx,ny);

index1 = 1;
for i = xmin: dx: xmax
    index2 = 1;
    for  j = ymin: dy : ymax
        C = 2.^i;
        scale = 2.^j;
        fres(index1,index2)= kfoldLoss(fitcsvm(Xtrain,Ytrain,'CVPartition',c,...
    'KernelFunction','rbf','BoxConstraint',C,...
    'KernelScale',scale,'Standardize',Standardize));
        index2 = index2+1;
    end
    index1 = index1+1;    
end

% fres;
minV = min(min(fres));

[row, col] = find(fres == minV);
z = 2.^[(dx*(row-1)+xmin), (dy*(col-1)+ymin)]
minV

close all;
contour(xmin:dx:xmax, ymin:dy:ymax,fres','showtext','on')

%%
rng(1);
searchmin= z(1,:);

minfn2 = @(z)kfoldLoss(fitcsvm(Xtrain,Ytrain,'CVPartition',c,...
    'KernelFunction','rbf','BoxConstraint',z(1),...
    'KernelScale',z(2),'Standardize',Standardize));

[searchmin, fval11] = fminsearch(minfn2,searchmin,opts)

%%
rng(1);
% BoxConstraint =z(1);
% KernelScale = z(2);
BoxConstraint =8;
KernelScale = 0.0313;
% BoxConstraint =2^(1.2);
% KernelScale = 6^(-5.6);

window = Ntest;
Xtrain2 = Xtrain;
Ytrain2 = Ytrain;
res = zeros(Ntest,3);

for i = 1: Ntest   
    SVMModel = fitcsvm(Xtrain2,Ytrain2,'KernelFunction','rbf','BoxConstraint'...
        ,BoxConstraint,'KernelScale',KernelScale,'Standardize',Standardize);
%     SVMModel = fitcsvm(Xtrain2,Ytrain2,'KernelFunction','rbf','BoxConstraint',BoxConstraint,'KernelScale',KernelScale);
    % 1. get predict result
    Xtest = X(Ntrain+i,1:end); %no window    
    Ytest = Y(Ntrain+i);    
    [label ,scores] = predict(SVMModel,Xtest);
    res(i,:) = [label Ytest scores(1)];
    
    % 2. update     
%     no window
    Xtrain2 = X(1:Ntrain+i,1:end);
    Ytrain2 = Y(1:Ntrain+i,:);    
    %window    
%     Xtrain2 = X(i:Ntrain+i,1:end);
%     Ytrain2 = Y(i:Ntrain+i,:);    
end


p11 = sum(res(:,1).*res(:,2))./ sum(res(:,2));
p00 = sum((~res(:,1)).*(~res(:,2)))./ sum(~res(:,2));

[sum(res(:,1)==res(:,2))./Ntest p11 p00 (p11+p00)/2 sum(~res(:,2))./Ntest]
%%
SVMModel = fitcsvm(Xtrain,Ytrain,'KernelFunction','rbf','BoxConstraint'...
        ,BoxConstraint,'KernelScale',KernelScale,'Standardize',Standardize);
[label1 ,scores1] = predict(SVMModel,X(Ntrain+1:end,:));    

res = [label1 Y(Ntrain+1:end) ];

p11 = sum(res(:,1).*res(:,2))./ sum(res(:,2));
p00 = sum((~res(:,1)).*(~res(:,2)))./ sum(~res(:,2));

[sum(res(:,1)==res(:,2))./Ntest p11 p00 (p11+p00)/2 sum(~res(:,2))./Ntest]
